package com.notecalendar;

import java.util.List;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;

import com.evernote.edam.notestore.NoteList;
import com.evernote.edam.type.Note;
import com.evernote.edam.type.Notebook;
import com.evernote.edam.type.User;
import com.notecalendar.bo.*;
import com.notecalendar.evernote.EvernoteUtil;
import com.notecalendar.evernote.exception.EvernoteBaseException;
import com.notecalendar.evernote.exception.EvernoteConsumerException;
import com.notecalendar.evernote.exception.EvernoteUserPassException;
import com.notecalendar.evernote.exception.EvernoteVersionException;
import com.notecalendar.exception.EvernoteBeanException;
import com.notecalendar.model.Evernote_Login;
import com.notecalendar.ws.beans.EvernoteUser;

/**
 * Class for actions over evernote relates beans
 * @author angelcereijo
 *
 */
public class EvernoteBean {
	
	//System log
	private Logger logger = Logger.getLogger(EvernoteBean.class);
	
	//injected via Spring
	private EvernoteUtil evernoteUtil = null;

	//injected via Spring
	private Evernote_FileBo evernote_FileBo;
	
	
	
	
	/**
	 * Init connection with evernote system
	 * @param evernoteUser
	 * @param evernotePass
	 * @throws EvernoteVersionException
	 * @throws EvernoteConsumerException
	 * @throws EvernoteUserPassException
	 * @throws EvernoteBaseException
	 */
	public EvernoteUser evernoteSystemInit(Evernote_Login evernote_login) throws EvernoteBeanException{
		//boolean connection = true;
		//String authToken  = null;
		EvernoteUser evernoteUser = new EvernoteUser();
		try{
			
			evernoteUtil.initializeElements(evernote_login);
			
			String authToken = evernoteUtil.getAuthToken();
			User user = evernoteUtil.getUser(); 
			evernoteUser.setAuthToken(authToken);
			evernoteUser.setUser(user.getName());
			evernoteUser.setEmail(user.getEmail());
			evernoteUser.setShardId(user.getShardId());
		} catch (EvernoteVersionException e) {
			logger.error(e);
			//conection = false;
			throw new EvernoteBeanException(EvernoteBeanException.CAN_NOT_INITIALIZED_EXCEPTION);
		} catch (EvernoteConsumerException e) {
			logger.error(e);
			//conection = false;
			throw new EvernoteBeanException(EvernoteBeanException.CAN_NOT_INITIALIZED_EXCEPTION);
		} catch (EvernoteUserPassException e) {
			logger.error(e);
			//conection = false;
			throw new EvernoteBeanException(EvernoteBeanException.CAN_NOT_INITIALIZED_EXCEPTION);
		} catch (EvernoteBaseException e) {
			logger.error(e);
			//conection = false;
			throw new EvernoteBeanException(EvernoteBeanException.CAN_NOT_INITIALIZED_EXCEPTION);
		}
		//return authToken;
		return evernoteUser;
	}
	
	/**
	 * Get notes from evernote
	 * @return
	 * @throws EvernoteBeanException
	 */
	public NoteList getNotes(String shardId,String auhToken) throws EvernoteBeanException{
		
		try{
			return evernoteUtil.getAllNotes(shardId,auhToken);
		} catch (EvernoteVersionException e) {
			logger.error(e);
			throw new EvernoteBeanException(EvernoteBeanException.CAN_NOT_GET_NOTES_EXCEPTION);
		} catch (EvernoteConsumerException e) {
			// TODO Auto-generated catch block
			logger.error(e);
			throw new EvernoteBeanException(EvernoteBeanException.CAN_NOT_GET_NOTES_EXCEPTION);
		} catch (EvernoteUserPassException e) {
			// TODO Auto-generated catch block
			logger.error(e);
			throw new EvernoteBeanException(EvernoteBeanException.CAN_NOT_GET_NOTES_EXCEPTION);
		} catch (EvernoteBaseException e) {
			// TODO Auto-generated catch block
			logger.error(e);
			throw new EvernoteBeanException(EvernoteBeanException.CAN_NOT_GET_NOTES_EXCEPTION);
		}catch(NullPointerException e){
			logger.error(e);
			throw new EvernoteBeanException(EvernoteBeanException.NOT_INITIALIZED_EXCEPTION);
		}
		
	}
	
	/**
	 * Create a new note on evernote system
	 * @param note
	 * @return
	 * @throws EvernoteBeanException
	 */
	public Note insertNote(Note note, String shardId)throws EvernoteBeanException{
		try{
			return evernoteUtil.createNote(note,shardId);
		}catch(NullPointerException e){
			logger.error(e);
			throw new EvernoteBeanException(EvernoteBeanException.NOT_INITIALIZED_EXCEPTION);
		} catch (EvernoteUserPassException e) {
			logger.error(e);
			throw new EvernoteBeanException(EvernoteBeanException.CREATE_NOTE_EXCEPTION);
		} catch (EvernoteBaseException e) {
			logger.error(e);
			throw new EvernoteBeanException(EvernoteBeanException.CREATE_NOTE_EXCEPTION);
		}
	}
	
	/**
	 * Send a note by email
	 * @param note
	 * @param addresses
	 * @param subject
	 * @param message
	 * @throws EvernoteBeanException
	 */
	public void sendNoteByEmail(Note note,List<String> addresses,String subject,String message, String shardId, String authToken)
		throws EvernoteBeanException{
		try{
			note = evernoteUtil.getCompleteNote(note, shardId, authToken);
			evernoteUtil.emailNote(note, addresses, subject, message, shardId, authToken);
		}catch (EvernoteUserPassException e) {
			logger.error(e);
			throw new EvernoteBeanException(EvernoteBeanException.NOT_INITIALIZED_EXCEPTION);
		}catch(EvernoteBaseException e){
			logger.error(e);
			throw new EvernoteBeanException(EvernoteBeanException.CREATE_EMAIL_NOTE);
		}
	}
	
	/**
	 * Create a new notebook
	 * @param notebook
	 * @param shardId
	 * @param authToken
	 * @throws EvernoteBeanException
	 */
	public void createNotebook(Notebook notebook, String shardId, String authToken) throws EvernoteBeanException{
		try {
			evernoteUtil.createNotebook(notebook, shardId, authToken);
		}catch (EvernoteUserPassException e) {
			logger.error(e);
			throw new EvernoteBeanException(EvernoteBeanException.NOT_INITIALIZED_EXCEPTION);
		}catch(EvernoteBaseException e){
			logger.error(e);
			throw new EvernoteBeanException(EvernoteBeanException.CREATE_EMAIL_NOTE);
		}
	}
	
	
	/**
	 * @return the evernoteUtil
	 */
	public EvernoteUtil getEvernoteUtil() {
		return evernoteUtil;
	}

	/**
	 * @param evernoteUtil the evernoteUtil to set
	 */
	public void setEvernoteUtil(EvernoteUtil evernoteUtil) {
		System.out.println("SET EvernoteUtil");
		this.evernoteUtil = evernoteUtil;
	}
	
	public Evernote_FileBo getEvernote_FileBo() {
		return evernote_FileBo;
	}

	public void setEvernote_FileBo(Evernote_FileBo evernote_FileBo) {
		System.out.println("SET Evernote_FileBo");
		this.evernote_FileBo = evernote_FileBo;
	}
	
}
